<!DOCTYPE html>

<html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>冒泡</title>
    <style>
        * {
            border: 1px solid gray;
            margin: 15px;
        }
        
        .outer,
        .middle,
        .inner {
            border-color: blue;
            margin: 100px;
        }
        
        .inner {
            height: 100px;
        }
    </style>
</head>

<body>

    <div class="outer">
        <div class="middle">
            <div class="inner"></div>
        </div>
    </div>

    <script type="text/javascript">
        (() => {
            let outer = document.querySelector('.outer');
            let middle = document.querySelector('.middle');
            let inner = document.querySelector('.inner');

            let type = 'click';

            let listener = function(e) {
                // 判断是否处于事件源上
                if (e.eventPhase === Event.AT_TARGET) {
                    console.log(Date.now(), this, '被点击');
                }
                // 判断是否处于冒泡阶段
                if (e.eventPhase === Event.BUBBLING_PHASE) {
                    console.log(Date.now(), '当前元素:', this);
                    console.log('事件源: ', e.target || e.srcElement);
                }
            }

            let capture = false; // flase 表示不在捕获阶段处理事件

            outer.addEventListener(type, listener, capture);
            middle.addEventListener(type, listener, capture);
            inner.addEventListener(type, listener, capture);

            document.body.addEventListener(type, listener, capture);
            document.lastElementChild.addEventListener(type, listener, capture); //document节点最后一个子元素html
            document.addEventListener(type, listener, capture); //document根节点
            window.addEventListener(type, listener, capture);
        })();
    </script>

</body>

</html>